<template>
  <div class="app-container">
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="打印标签" width="550px">
      <el-form ref="form" :model="form" label-width="100px">
        <!-- <el-form-item label="选择打印机">
          <el-select clearable v-model="printCode" @change="addPrint">
            <el-option
              v-for="item in printers"
              :key="item.value"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="总数量">
          <el-input v-model="form.number"/>
        </el-form-item>
        <el-form-item label="初始数量">
          <el-input v-model="form.startNum" type="number"/>
        </el-form-item>
        <el-form-item label="结束数量">
          <el-input v-model="form.endNum" type="number"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="printFirstContent">首尾页打印</el-button>
        <el-button type="primary" @click="printContent">筛选打印</el-button>
        <el-button @click="open=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getLodop} from '@/utils/LodopFuncs';
import {printOrder} from '@/api/order/order';

let LODOP;

export default {
  name: 'SinglePrintCode',
  data() {
    return {
      open: false,
      dataArray: [],
      form: {
        printCode: null,
        number: null,
        startNum: null,
        endNum: null,
      },
      printerList: [],
      printers: []
    }
  },
  created() {
    LODOP = getLodop()
    // 获取打印机个数
    let counter = LODOP.GET_PRINTER_COUNT()
    for (let i = 0; i < counter; i++) {
      this.printerList.push({
        // 将打印机存入printerList数组中
        name: this.LODOP.GET_PRINTER_NAME(i),
        value: i
      })
    }

  },
  methods: {
    openDialog(id) {
      this.open = true;
      printOrder(id).then(res => {
        var data = res.data[0]
        this.form.number = data.number
        this.form.startNum = 1
        this.form.endNum = data.number
        this.dataArray = data
      })
    },
    printFirstContent() {
      // 获取LODOP对象的主过程
      LODOP = getLodop()
      if (!LODOP) {
        this.$message.error('打印错误，请检查打印机是否安装正确！')
        return
      }
      var datas = this.dataArray
      var start = 1
      var end = datas.number
      // 打印首尾两次
      LODOP.PRINT_INITA(-1, -1, 315, 195, "标签打印");
      LODOP.SET_PRINT_MODE('PRINT_NOCOLLATE', 1)

      for (let i = 1; i <= 2; i++) {
        LODOP.NewPage()
        LODOP.SET_PRINT_PAGESIZE(1, 800, 500, "B5");
        LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1);
        LODOP.ADD_PRINT_TEXT(9, 6, 85, 20, datas.time);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        LODOP.ADD_PRINT_TEXT(8, 92, 51, 20, datas.transportPro);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
        LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
        LODOP.SET_PRINT_STYLEA(0, "TextFrame", 5);
        LODOP.ADD_PRINT_TEXT(30, 6, 80, 30, datas.startOrg);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
        LODOP.ADD_PRINT_TEXT(24, 84, 49, 34, "→");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 25);
        LODOP.ADD_PRINT_TEXT(31, 130, 77, 30, datas.endOrg);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
        LODOP.ADD_PRINT_TEXT(7, 136, 142, 24, '单号:' + datas.code);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
        LODOP.ADD_PRINT_TEXT(103, 7, 188, 20, '收货人:' + datas.SHF.contactperson);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        LODOP.SET_PRINT_STYLEA(0, "TextFrame", 2);
        LODOP.ADD_PRINT_TEXT(56, 5, 185, 20, datas.FHF.contactperson);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        if (i === 1) {
          LODOP.ADD_PRINT_TEXT(76, 7, 172, 24, datas.barcodes[0].productName + '-件数:共' + datas.number + '-第' + i);
        } else {
          LODOP.ADD_PRINT_TEXT(76, 7, 172, 24, datas.barcodes[end - 1].productName + '-件数:共' + datas.number + '-第' + end);
        }
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
        LODOP.ADD_PRINT_TEXT(159, 8, 150, 30, "客户单号:O2024040100106");
        LODOP.ADD_PRINT_RECT(73, 6, 138, 22, 0, 1);
        if (i === 1) {
          LODOP.ADD_PRINT_BARCODE(82, 200, 103, 154, "QRCode", datas.barcodes[0].productCode);
        } else {
          LODOP.ADD_PRINT_BARCODE(82, 200, 103, 154, "QRCode", datas.barcodes[end - 1].productCode);
        }
        LODOP.SET_PRINT_STYLEA(0, "QRCodeVersion", 2);
        LODOP.ADD_PRINT_TEXT(123, 7, 189, 36, "地址:" + datas.SHF.detaileAddress);
        LODOP.ADD_PRINT_TEXT(158, 156, 133, 31, "备注：");
        LODOP.ADD_PRINT_TEXT(30, 207, 75, 40, "云南省,昆明市,五华区");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
      }
      // LODOP.PRINT();
      // LODOP.PREVIEW()
      LODOP.PRINT_DESIGN();
      this.open = false
    },
    printContent() {
      // 获取LODOP对象的主过程
      LODOP = getLodop()
      if (!LODOP) {
        this.$message.error('打印错误，请检查打印机是否安装正确！')
        return
      }
      var datas = this.dataArray
      var start = this.form.startNum
      var end = this.form.endNum

      if (start < 1 || end < 1) {
        this.$message.error('数量不能小于1！')
      }
      if (!datas) {
        this.$message.error('请先查询数据！')
      }


      LODOP.PRINT_INITA(-1, -1, 300, 265, "标签打印");
      LODOP.SET_PRINT_MODE('PRINT_NOCOLLATE', 1)

      for (let i = start; i <= end; i++) {
        LODOP.NewPage()
        LODOP.SET_PRINT_PAGESIZE(2, 800, 800, "B5");
        LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1);
        LODOP.ADD_PRINT_TEXT(36, 10, 85, 20, datas.time);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        LODOP.ADD_PRINT_TEXT(35, 92, 51, 20, datas.transportPro);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
        LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
        LODOP.SET_PRINT_STYLEA(0, "TextFrame", 5);
        LODOP.ADD_PRINT_TEXT(57, 9, 80, 30, datas.startOrg);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
        LODOP.ADD_PRINT_TEXT(51, 84, 49, 34, "→");
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 25);
        LODOP.ADD_PRINT_TEXT(58, 130, 77, 30, datas.endOrg);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
        LODOP.ADD_PRINT_TEXT(34, 136, 157, 24, '单号:' + datas.code);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 11);
        LODOP.ADD_PRINT_TEXT(129, 8, 285, 20, '收货人:' + datas.SHF.contactperson + ' ' + datas.SHF.iphone);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        LODOP.SET_PRINT_STYLEA(0, "TextFrame", 2);
        LODOP.ADD_PRINT_BARCODE(159, 20, 105, 109, "QRCode", datas.barcodes[i - 1].productCode);
        LODOP.SET_PRINT_STYLEA(0, "QRCodeVersion", 2);
        LODOP.ADD_PRINT_TEXT(154, 104, 189, 56, datas.SHF.detaileAddress);
        LODOP.ADD_PRINT_TEXT(83, 8, 210, 20, datas.FHF.contactperson + ' ' + datas.FHF.iphone);
        LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
        LODOP.ADD_PRINT_TEXT(106, 7, 136, 24, datas.barcodes[i - 1].productName + '-件数:' + datas.number + '-' + i);
        LODOP.ADD_PRINT_IMAGE(57, 220, 72, 71, '<img src=\'https://file.bh-express.cn/gh_0769bf0c95e6_344.jpg\'/>');
        LODOP.SET_PRINT_STYLEA(0, "Stretch", 1);
        LODOP.ADD_PRINT_RECT(207, 107, 185, 58, 0, 1);
        LODOP.ADD_PRINT_TEXT(209, 108, 59, 20, "客户签名");
        LODOP.ADD_PRINT_TEXT(244, 196, 95, 20, "年    月    日");
        LODOP.ADD_PRINT_TEXT(242, 12, 40, 20, "实收:    ");
        LODOP.ADD_PRINT_RECT(104, 6, 137, 17, 0, 1);
        LODOP.ADD_PRINT_RECT(237, 9, 96, 28, 0, 1);
      }
      LODOP.PRINT();
      // LODOP.PREVIEW()
      // LODOP.PRINT_DESIGN();
      this.open = false
    },
    printByCustomer() {
      directPrint(this.form).then((response) => {
        console.log(response.data)
        this.pdfFileName = ["D:\\pdf\\hello_0.jpg","D:\\pdf\\hello_1.jpg","D:\\pdf\\hello_2.jpg"];
        console.log(this.pdfFileName.length)
        if (response.code == 200) {
          const strURL = this.pdfFileName; // 打印图片的地址
          LODOP = getLodop();
          //打印初始化
          LODOP.PRINT_INIT("打印");
          //设置纸张大小
          LODOP.SET_PRINT_PAGESIZE(3, 1000, 1500, "");
          //传入打印的，实现的多张图片的打印
          for( var i=0;i<this.pdfFileName.length;i++){
            LODOP.NewPage();
            LODOP.ADD_PRINT_IMAGE(0, 0, "100%", "100%",this.pdfFileName[i]);
          }

          LODOP.SET_PREVIEW_WINDOW(0, 0, 0, 0, 0, "");

          console.log(this.form.attr3);
          //选择打印机this.form.attr3，这是我自己获取到打印机的地址

          if (LODOP.SET_PRINTER_INDEXA(this.form.attr3)) {
            //预览打印
            LODOP.PREVIEW();
          }

          this.$modal.msgSuccess("打印成功");

        }
      });

    }
  }
}
</script>
